//
//  VisiterView.swift
//  weibo
//
//  Created by yellow on 15/10/15.
//  Copyright © 2015年 yellow. All rights reserved.
//

import UIKit
import SnapKit

//定义点击按钮事件代理
protocol VisiterViewProtocol:NSObjectProtocol{

    //点击注册按钮
    func visiterViewDidClickRegisterBtn();
    //点击登录按钮
    func visiterViewDidClickLoginBtn();
}

class VisiterView: UIView {
    
    /*
     代理属性使用weak var修饰，因为代理使用强引用会产生循环引用，var是代理都是变化的，
     delegate:VisiterViewProtocol 代理类型指定和变量类型指定方式一样
     */
    weak var delegate:VisiterViewProtocol?
    
    // MARK: - 按钮事件处理
    @objc private func registerBtnClick(){
        delegate?.visiterViewDidClickRegisterBtn()
    }
    @objc private func loginBtnClick(){
        delegate?.visiterViewDidClickLoginBtn()
    }
    
    
    
    // MARK: - 初始设置
    override init(frame: CGRect) {
        super.init(frame: frame)
        addWidget()
    }

    func setInfo(imageName:String?,tips:String?){
        //守卫非首页的view的图片
        guard let imgName = imageName else{
            startAnimal()
            return 
        }
        scrollIcon.hidden = true
        houseIcon.image = UIImage(named: imgName)
        tipLable.text = tips
    }
    
    // initWithCoder - 使用 SB & XIB 开发加载的函数
    // 使用 sb 开始的入口
    required init?(coder aDecoder: NSCoder) {
        // 导致如果使用 SB 开发，调用这个视图，会直接崩溃
        fatalError("init(coder:) has not been implemented")
//        super.init(coder: aDecoder)
    }
    
    // MARK: - 懒加载
    
    ///背景图片
    private lazy var maskImage:UIImageView = UIImageView(imageName:"visitordiscover_feed_mask_smallicon")

    
    ///转圈图片
    private lazy var scrollIcon:UIImageView = UIImageView(imageName:"visitordiscover_feed_image_smallicon")
    
    ///房子图片
    private lazy var houseIcon:UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    /// 提示信息lable
    private lazy var tipLable:UILabel = {
        let lable = UILabel()
        lable.text = "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知"
        lable.textColor = UIColor.darkGrayColor()
        lable.font = UIFont.systemFontOfSize(12)
        lable.numberOfLines = 0
        lable.textAlignment = NSTextAlignment.Center
        return lable
        }()
    
    /// 注册按钮
    private lazy var registerBtn:UIButton = UIButton(title: "注册",color:UIColor.orangeColor(), imageName: "common_button_white_disable", actionName:"registerBtnClick",target:self)
    
    /// 登录按钮
    private lazy var loginBtn:UIButton = UIButton(title: "登录",color:UIColor.orangeColor(),imageName: "common_button_white_disable", actionName:"loginBtnClick",target:self)

}

// MARK: - 页面设置
extension VisiterView {

/// 添加控件
    private func addWidget(){
        addSubview(scrollIcon)
        addSubview(maskImage)
        addSubview(houseIcon)
        addSubview(tipLable)
        addSubview(registerBtn)
        addSubview(loginBtn)
        autoLayoutWidgets()
    }
    
    //在旋转图片的layer上开启动画
    private func startAnimal(){
        let animal = CABasicAnimation(keyPath: "transform.rotation")
        animal.toValue = 2 * M_PI
        animal.repeatCount = Float.infinity// or MAXFLOAT
        animal.duration = 20
        /* When true, the animation is removed from the render tree once its
        * active duration has passed. Defaults to true. */
        animal.removedOnCompletion = false//是否当进入其他页面，动画是否暂停
        scrollIcon.layer.addAnimation(animal, forKey: nil)
    }
    
/// 自动布局控件
    private func autoLayoutWidgets(){
        
        //使用snapkit框架进行自动布局，不需要设置translatesAutoresizingMaskIntoConstraints = false
        
        //背景图片
        maskImage.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(self.snp_width)
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(registerBtn.snp_bottom)
        }
    
        
        // 设置背景颜色 - 灰度图 R = G = B，在 UI 元素中，大多数都使用灰度图，或者纯色图(安全色)
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)

        
        //转圈图片
        scrollIcon.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.center)
        }

        
        //房子图片
        houseIcon.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }

        
        //提示信息lable
        tipLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(130)
            make.width.equalTo(224)
            make.height.equalTo(36)
        }
    
        //注册按钮
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLable.snp_left)
            make.top.equalTo(tipLable.snp_top).offset(50)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        
        
        
        //登录按钮
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLable.snp_right)
            make.top.equalTo(tipLable.snp_top).offset(50)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
    }
}
